package algorithm.doublePointer;

// 快乐数
public class LC202 {
    public boolean isHappy(int n) {
        int slow = n, fast = bitSum(n);

        while (fast != slow) {
            fast = bitSum(bitSum(fast));
            slow = bitSum(slow);
        }

        return slow == 1;
    }

    private int bitSum(int n) {
        int sum = 0;
        while (n != 0) {
            int a = n % 10;
            sum += a * a;
            n /= 10;
        }
        return sum;
    }
}
